//
//  MayhemServiceClient.m
//  Mayhem
//
//  Created by Jay Meistrich on 5/27/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MayhemServiceClient.h"
#import <CFNetwork/CFNetwork.h>

@implementation MayhemServiceClient

- (MayhemServiceClient*) initWithUrl:(NSString*)url
{
    self = [super init];
    if (self)
    {
        urlBase = url;
    }
    return self;
}

- (void) EventAsync:(NSString *)url
{
    NSString* eventUrl = [urlBase stringByAppendingFormat:@"/Event/%@",url];

    urlRequest =
    [NSURLRequest requestWithURL:[NSURL URLWithString:eventUrl]];
    
    [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}

- (void) GetFormDataAsync:(BOOL)force
{
    NSString* eventUrl = [urlBase stringByAppendingFormat:@"/GetFormData?force=%@",(force?@"true":@"false")];
    
    urlRequest =
    [NSURLRequest requestWithURL:[NSURL URLWithString:eventUrl]];
    
    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    
    if (connection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = [[NSMutableData data] retain];
    } else {
        // Inform the user that the connection failed.
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // This method is called when the server has determined that it
    // has enough information to create the NSURLResponse.
    
    // It can be called multiple times, for example in the case of a
    // redirect, so each time we reset the data.
    
    // receivedData is an instance variable declared elsewhere.
    if(receivedData != nil)
    {
        [receivedData setLength:0];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [receivedData release];
    receivedData = nil;
    
    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ServiceGetDataCompleted" object:receivedData];
    
    // release the connection, and the data object
    [connection release];
    [receivedData release];
    receivedData = nil;
}

@end
